/*
 * Copyright(C) 2010 Luvina Software Company
 *
 * LoginFilter.java, Oct 29, 2010 ntmhuong
 */
package net.luvina.manageuser.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.luvina.manageuser.managedbean.LoginSession;

/**
 * 認証セ�?��ョン確認用フィルター
 *
 * @version $Rev$
 */
public class LoginFilter implements Filter {
    private static final String LOGIN_PATH = "/login.do";
    private static final String LOGIN_URL = "/manageuser/login.do";

    /**
     * 認証セ�?��ョンの有無を確認するフィルター実�?
     *
     * @param request
     *            ServletReqest
     * @param response
     *            ServletResponse
     * @param chain
     *            FilterChain
     * @exception IOException
     *                Redirect時�?次のFilter実行時に発�?
     * @exception ServletException
     *                次のFilter実行時に発�?
     */
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        if (!LOGIN_PATH.equals(((HttpServletRequest) request).getServletPath())) {
            HttpSession session = ((HttpServletRequest) request).getSession();
            LoginSession loginSession = (LoginSession) session
                    .getAttribute("loginSession");
            //
            if (loginSession == null || !loginSession.isLogin()) {
                ((HttpServletResponse) response).sendRedirect(LOGIN_URL);
                return;
            } else {
                // �?��操作時間�?更新
                // loginSession.setLastOperateDatetime(new Date());
            }
        }

        chain.doFilter(request, response);
    }

    /**
     * Filter実行前処�?
     *
     * @param config
     *            FilterConfig
     */
    public void init(FilterConfig config) throws ServletException {
    }

    /**
     * Filter実行後�?�?
     */
    public void destroy() {
    }
}
